********************************************************************************
*************************** Do File (4): Table 3 *******************************
********************************************************************************

clear
matrix drop _all
set more off

use "$data/EPH_1015_format.dta", clear

destring occupation occupation_unemp occup_all, replace

*** Keep only observations that have all controls used

keep if (occupation !=. & underemp != . & cont_pens !=. & domwk_all !=. & linc_mjob_base08 !=. & lwagehr_mjob_base08 !=. & linc_total_base08 != . & lhours_mjob !=. & age !=. & hhsize !=. & tenure !=. & msa !=. & educyr !=. ///
& attsch_ever !=. & native !=. & lit !=. & gender !=.) | (occupation_unemp != . & age !=. & hhsize !=. & msa !=. & educyr !=. & attsch_ever !=. & native !=. & lit !=. & gender !=.)

********************************************************************************
********************************************************************************

*** Locals for controls

local occup occupation
local base_controls "msa year"
local controls "age age2 hhsize lit native attsch_ever educyr educyr2 i.marstat i.dec_pcfaminc"


*** Label the variables as they appear in the table

label var cont_pens "Registered"
label var unemployed "Unemployed"
label var lhours_mjob "Hours of work per week on main job"
label var underemp "Underemployment"
label var linc_mjob_base08 "Income per month from main job"
label var lwagehr_mjob_base08 "Wage per hour from main job"

********************************************************************************
********************************************************************************
********************************************************************************

* First run all the regressions to get the p-values and obtain adjusted p-values

qui reghdfe cont_pens domwk_all treat_dwall `controls' if ${ctrl_group} == 1 & unemployed == 0, absorb(`base_controls' `occup') vce(cluster msa)
gen sample_reg = 1 if e(sample)
matrix define uPtreat = 2*ttail(e(N),abs(_b[treat_dwall]/_se[treat_dwall]))

qui reghdfe unemployed domwk_all treat_dwall `controls' if ${ctrl_group} == 1, absorb(`base_controls' occup_all) vce(cluster msa)
matrix uPtreat = uPtreat,2*ttail(e(N),abs(_b[treat_dwall]/_se[treat_dwall]))

foreach var in lhours_mjob underemp linc_mjob_base08 lwagehr_mjob_base08 {

	qui reghdfe `var' domwk_all treat_dwall `controls' if sample_reg == 1, absorb(`base_controls' `occup') vce(cluster msa)
	matrix uPtreat = uPtreat,2*ttail(e(N),abs(_b[treat_dwall]/_se[treat_dwall]))
		
}

* Calculate adjusted p-values

matrix uPtreat = uPtreat'
svmat uPtreat, names(unad_p)

qqvalue unad_p1, method(hochberg) qvalue(hochbergP_treat)

qui sum unad_p1
mkmat unad_p1 hochbergP_treat if _n <= r(N), mat(adjPval)
matrix adjPval = adjPval[1..r(N),2]

matrix colnames adjPval = hochberg_p
matrix rownames adjPval = cont_pens unemployed lhours_mjob underemp linc_mjob_base08 lwagehr_mjob_base08


** Rerun all the regressions, including the adjusted p-values

local i = 1

* Formality - Contribution to pension system

local varlabel: variable label cont_pens
qui reghdfe cont_pens domwk_all treat_dwall `controls' if sample_reg == 1, absorb(`base_controls' `occup') vce(cluster msa)
sum cont_pens if domwk_all == 1 & treat == 0 & e(sample)
local meanvar = r(mean)
local qval = round(adjPval[`i',1], 0.001)
outreg2 using "$tables/Table_3", replace excel keep(treat_dwall) nocons dec(3) label ctitle(`varlabel') addstat(Mean dependent variable,`meanvar', q-value, `qval') ///
addtext(Year Fixed Effects, Yes, Occupation Fixed Effects, Yes, Metropolitan Area Fixed Effects, Yes, Number of clusters, "`e(N_clust)'")
local i = `i' + 1

* Unemployment

local varlabel: variable label unemployed
qui reghdfe unemployed domwk_all treat_dwall `controls' if ${ctrl_group} == 1, absorb(`base_controls' occup_all) vce(cluster msa)
sum unemployed if domwk_all == 1 & treat == 0 & e(sample)
local meanvar = r(mean)
local qval = round(adjPval[`i',1], 0.001)
outreg2 using "$tables/Table_3", append excel keep(treat_dwall) nocons dec(3) label ctitle(`varlabel') addstat(Mean dependent variable,`meanvar', q-value, `qval') ///
addtext(Year Fixed Effects, Yes, Occupation Fixed Effects, Yes, Metropolitan Area Fixed Effects, Yes, Number of clusters, "`e(N_clust)'")
local i = `i' + 1

* Log hours of work per week

local varlabel: variable label lhours_mjob
qui reghdfe lhours_mjob domwk_all treat_dwall `controls' if sample_reg == 1, absorb(`base_controls' `occup') vce(cluster msa)
sum hours_mjob if domwk_all == 1 & treat == 0 & e(sample)
local meanvar r(mean)
local qval = round(adjPval[`i',1], 0.001)
outreg2 using "$tables/Table_3", append excel keep(treat_dwall) nocons dec(3) label ctitle(`varlabel') addstat(Mean dependent variable,`meanvar', q-value, `qval') ///
addtext(Year Fixed Effects, Yes, Occupation Fixed Effects, Yes, Metropolitan Area Fixed Effects, Yes, Number of clusters, "`e(N_clust)'")
local i = `i' + 1

* Underemployment (working fewer hours than desired)

local varlabel: variable label underemp
qui reghdfe underemp domwk_all treat_dwall `controls' if sample_reg == 1, absorb(`base_controls' `occup') vce(cluster msa)
sum underemp if domwk_all == 1 & treat == 0 & e(sample)
local meanvar r(mean)
local qval = round(adjPval[`i',1], 0.001)
outreg2 using "$tables/Table_3", append excel keep(treat_dwall) nocons dec(3) label ctitle(`varlabel') addstat(Mean dependent variable,`meanvar', q-value, `qval') ///
addtext(Year Fixed Effects, Yes, Occupation Fixed Effects, Yes, Metropolitan Area Fixed Effects, Yes, Number of clusters, "`e(N_clust)'")
local i = `i' + 1


* Earnings

foreach var in inc_mjob_base08 wagehr_mjob_base08 {

	local varlabel: variable label `var'
	qui reghdfe l`var' domwk_all treat_dwall `controls' if sample_reg == 1, absorb(`base_controls' `occup') vce(cluster msa)
	sum `var' if domwk_all == 1 & treat == 0 & e(sample)
	local meanvar r(mean)
	local qval = round(adjPval[`i',1], 0.001)
	outreg2 using "$tables/Table_3", append excel keep(treat_dwall) nocons dec(3) label ctitle(`varlabel') addstat(Mean dependent variable,`meanvar', q-value, `qval') ///
	addtext(Year Fixed Effects, Yes, Occupation Fixed Effects, Yes, Metropolitan Area Fixed Effects, Yes, Number of clusters, "`e(N_clust)'")
	local i = `i' + 1
	
}

exit